<script type="text/javascript">
$(function(){
	var pg = App.codex.macFlowWrapHandler.ui, pp = pg.params || {}
		, hd = pg.children('.head'), bd = pg.children('.body');
	
	var frm = pg.frm = {
		appCb: pg.seek('appCb'),
		codeCb: pg.seek('codeCb'),
		key: pg.seek('key'),
		faceCb: pg.seek('faceCb'),
		methodCb: pg.seek('methodCb')
	};
	
	var appGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'name', width: 200,
			title: '应用名'
		}],
		loader: {
			url: '/soagov/app/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.appCb.mac('combo', {
		keyField: 'id',
		displayField: 'name',
		multiSelect: false,
		allowInput: true,
		width: 532,
		boxLeft: 0,
		boxWidth: 532,
		boxHeight: 320,
		boxEl: appGd,
		onSelect: function(me, sk, sd){
			var r = sd[0] || {};
			if (r.id) {
				pp.appId = r.id;
			}
		}
	});
	frm.appCb.mac('combo_grid', {});
	
	var codeGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'code', width: 200,
			title: '类名'
		},{
			field: 'key', width: 120,
			title: '识别码'
		}],
		loader: {
			url: App.ctx + '/mac/flow/wrap/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.codeCb.mac('combo', {
		keyField: 'code',
		displayField: 'code',
		multiSelect: false,
		allowInput: true,
		width: 532,
		boxLeft: 0,
		boxWidth: 532,
		boxHeight: 320,
		boxEl: codeGd,
		onSelect: function(me, sk, sd){
			var r = sd[0] || {};
			if (r.id) {
				pp.id = r.id;
				if (r.type == 1) {
					pp.type = r.type;
					frm.key.val(r.key);
					frm.key.removeAttr('disabled');
					return;
				}
			}
			pp.type = 0;
			frm.key.val('default');
			frm.key.attr('disabled', true);
		}
	});
	frm.codeCb.mac('combo_grid', {});
	
	var faceGd = $('<div class="grid"></div>').mac('grid', {
		key: 'code',
		cols : [{
			field: 'code', title : '接口英文名', width: 140,
			render: function(r){
				return $('<span class="a"></span>').click(function(){
					App.open({
						ctx: 'soagov',
						action: 'faceView',
						params: { id: r.id }
					});
				}).append(r.code);
			}
		},{
			field: 'name', title : '接口中文名', width: 240
		},{
			field: 'ver', title : '版本', width: 80
		}],
		loader: {
			url: '/soagov/face/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.faceCb.mac('combo', {
		keyField: 'id',
		displayField: 'face',
		multiSelect: false,
		width: 532,
		boxLeft: 0,
		boxWidth: 532,
		boxHeight: 320,
		boxEl: faceGd,
		onSelect: function(me, sk, sd){
			var r = sd[0] || {};
			if (r.id) {
				pp.faceId = r.id;
				pp.faceCode = r.code;
				methodGd.val('');
				methodGd.load({ faceId: r.id });
			}
		}
	});
	frm.faceCb.mac('combo_grid', {});
	
	var methodGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'code', title : '方法声明', width: 320
		},{
			field: 'memo', title : '方法说明', width: 320
		}],
		loader: {
			url: '/code/face/method/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: false
		},
		pagerLength: 10
	});
	frm.methodCb.mac('combo', {
		keyField: 'id',
		displayField: 'code',
		multiSelect: false,
		width: 806,
		boxLeft: 0,
		boxWidth: 806,
		boxHeight: 320,
		boxEl: methodGd,
		onSelect: function(me, sk, sd){
			var r = sd[0] || {};
			if (r.id) {
				pp.methodId = r.id;
				pp.methodCode = r.code;
				// 查询返回值和参数
				initCheckOut(r);
				$.post(App.codex.ctx + '/method/param/list.do', {
					key: 'mac/flow/WrapHandler/0/',
					methodId: r.id,
					pageNo: 1,
					pageSize: 50
				}, function(rs){
					var ro = mac.eval(rs);
					if (ro.success) {
						initCheckIn(ro.data);
					} else {
						mac.alert(ro.data, ro.params);
					}
				});
			}
		}
	});
	frm.methodCb.mac('combo_grid', {});
	
	function initCheckIn(data) {
		var args = pg.seek('args').empty();
		$.each(data.list, function(i, a) {
			var pl = pg.seek('arg').clone(), gd = pl.seek('grid');
			pl.find('legend').text('参数' + i);
			pl.seek('argType').text(a.type);
			pl.attr('name', a.name).show();
			//
			gd.mac('grid', {
				cols : [{
					field: 'toObj', title : '目标对象', width: 120,
					render: function(r, tr, gd) {
						return $('<span class="a"></span>')//
							.append(r.toObj).attr('hint', r.toObjType);
					}
				},{
					field: 'toProp', title : '目标属性', width: 120,
					render: function(r, tr, gd) {
						return $('<span class="a"></span>')//
							.append(r.toProp).attr('hint', r.toPropType)
					}
				},{
					field: 'fromObj', title : '来源对象', width: 120,
					render: function(r, tr, gd) {
						return $('<span class="a"></span>')//
							.append(r.fromObj).attr('hint', r.fromObjType);
					}
				},{
					field: 'fromProp', title : '来源属性', width: 120
				},{
					field: 'id', title : '操作', width: 100,
					render: function(r, tr, gd) {
						var p = $('<div></div>');
						if (!$.inArray(r.fromType, Cache.code.java.type)) {
							$('<span class="a"></span>').click(function(){
								App.open({
									ctx: 'codex',
									action: 'beanMappingAdd',
									params: r
								}, function(){
									gd.load({ codeId: pp.id });
								});
							}).append('映射').appendTo(p);
							p.append('<span>&nbsp;&nbsp;</span>');
						}
						$('<span class="a"></span>').click(function(){
							App.open({
								ctx: 'codex',
								action: 'beanMappingEdit',
								params: r
							}, function(){
								gd.load({ codeId: pp.id });
							});
						}).append('编辑').appendTo(p);
						p.append('<span>&nbsp;&nbsp;</span>');
						$('<span class="a"></span>').click(function(){
							mac.confirm('确定要 删除 此映射吗？', function(){
								$.post(App.codex.ctx + '/bean/mapping/delete.do', {
									id: r.id
								}, function(rs){
									var ro = mac.eval(rs);
									if (ro.success) {
										gd.load();
									} else {
										mac.alert(ro.data, ro.params);
									}
								});
							});
						}).append('删除').appendTo(p);
						return p;
					}
				}],
				loader: {
					url: App.codex.ctx + '/bean/mapping/list.do',
					params: {
						pageNo: 1, pageSize: 50,
						codeId: pp.id,
						key: 'mac/flow/WrapHandler/0/' + i
					},
					autoLoad: true
				},
				pagerLength: 10
			}).adjust();
			
			// if (App.logon.power('code.mac.flow.save')){
				pl.seek('add').click(function(){
					// 添加前先保存
					var pms = getValues();
					if (!validate(pms)) {
						return;
					}
					$.post(App.codex.ctx + '/mac/flow/wrap/save.do', pms, function(rs){
						var ro = mac.eval(rs);
						if (ro.success) {
							pp.id = ro.data;
							App.open({
								ctx: 'codex',
								action: 'beanMappingAdd',
								params: {
									codeId: pp.id,
									key: 'mac/flow/WrapHandler/0/' + i,
									toType: a.type
								}
							}, function(){
								gd.load({ codeId: pp.id });
							});
						} else {
							pg.seek('msg').html(mac.msg(ro.data, ro.params));
						}
					});
				});
				pl.seek('add1').click(function(){
					// 添加前先保存
					var pms = getValues();
					if (!validate(pms)) {
						return;
					}
					$.post(App.codex.ctx + '/mac/flow/wrap/save.do', pms, function(rs){
						var ro = mac.eval(rs);
						if (ro.success) {
							pp.id = ro.data;
							App.open({
								ctx: 'codex',
								action: 'beanMappingEdit',
								params: {
									codeId: pp.id,
									key: 'mac/flow/WrapHandler/0/' + i,
									toType: a.type
								}
							}, function(){
								gd.load({ codeId: pp.id });
							});
						} else {
							pg.seek('msg').html(mac.msg(ro.data, ro.params));
						}
					});
				});
			// }
			args.append(pl);
		});
	}
	
	function initCheckOut(method){
		var pl = pg.seek('checkOut'), gd = pl.seek('grid');
		if (frm.rtnType) {
			frm.rtnType.text(method.rtnType);
			gd.load({ codeId: pp.id })
			return;
		}
		frm.rtnType = pl.seek('rtnType').text(method.rtnType);
		//
		gd.mac('grid', {
			cols : [{
				field: 'toObj', title : '目标对象', width: 120,
				render: function(r, tr, gd) {
					return $('<span class="a"></span>')//
						.append(r.toObj).attr('hint', r.toObjType);
				}
			},{
				field: 'toProp', title : '目标属性', width: 120,
				render: function(r, tr, gd) {
					return $('<span class="a"></span>')//
						.append(r.toProp).attr('hint', r.toPropType)
				}
			},{
				field: 'fromObj', title : '来源对象', width: 120,
				render: function(r, tr, gd) {
					return $('<span class="a"></span>')//
						.append(r.fromObj).attr('hint', r.fromObjType);
				}
			},{
				field: 'fromProp', title : '来源属性', width: 120
			},{
				field: 'id', title : '操作', width: 100,
				render: function(r, tr, gd) {
					var p = $('<div></div>');
					if (!$.inArray(r.fromType, ['java.lang.String'])) {
						$('<span class="a"></span>').click(function(){
							App.open({
								ctx: 'codex',
								action: 'beanMappingEdit',
								params: { codeId: pp.id, fromObj: r.fromObj }
							}, function(){
								gd.load({ codeId: pp.id });
							});
						}).append('映射').appendTo(p);
						p.append('<span>&nbsp;&nbsp;</span>');
					}
					$('<span class="a"></span>').click(function(){
						App.open({
							ctx: 'codex',
							action: 'beanMappingEdit',
							params: r
						}, function(){
							gd.load({ codeId: pp.id });
						});
					}).append('编辑').appendTo(p);
					p.append('<span>&nbsp;&nbsp;</span>');
					$('<span class="a"></span>').click(function(){
						mac.confirm('确定要 删除 此映射吗？', function(){
							$.post(App.codex.ctx + '/bean/mapping/delete.do', {
								id: r.id
							}, function(rs){
								var ro = mac.eval(rs);
								if (ro.success) {
									gd.load();
								} else {
									mac.alert(ro.data, ro.params);
								}
							});
						});
					}).append('删除').appendTo(p);
					return p;
				}
			}],
			loader: {
				url: App.codex.ctx + '/bean/mapping/list.do',
				params: {
					pageNo: 1, pageSize: 50,
					codeId: pp.id,
					key: 'mac/flow/WrapHandler/1/0'
				},
				autoLoad: true
			},
			pagerLength: 10
		}).adjust();
		
		// if (App.logon.power('code.mac.flow.save')){
			pl.seek('add').click(function(){
				// 添加前先保存
				var pms = getValues();
				if (!validate(pms)) {
					return;
				}
				$.post(App.codex.ctx + '/mac/flow/wrap/save.do', pms, function(rs){
					var ro = mac.eval(rs);
					if (ro.success) {
						pp.id = ro.data;
						App.open({
							ctx: 'codex',
							action: 'beanMappingAdd',
							params: {
								codeId: pp.id,
								key: 'mac/flow/WrapHandler/1/0',
								toType: method.rtnType
							}
						}, function(){
							gd.load({ codeId: pp.id });
						});
					} else {
						pg.seek('msg').html(mac.msg(ro.data, ro.params));
					}
				});
			}).show();
		// }
	}

	function adjust(){
		var h = pg.parent().height();
		bd.height(h - hd.height() - 48);
		// gd.adjust();
	}
	$(window).resize(adjust);
	adjust();

	if (pp.id) {
		var w = mac.wait('Loading');
		try {
			$.get(App.codex.ctx + '/mac/flow/wrap/get.do', {
				id: pp.id
			}, function(rs) {
				var ro = mac.eval(rs);
				if (ro.success) {
					initCheckIn(ro.data);
					initCheckOut(ro.data);
				} else {
					pg.seek('msg').html(mac.msg(ro.data, ro.params));
				}
			});
		} finally {
			w.close();
		}
	}
	
	function getValues(){
		return $.extend(pp, {
			code: frm.codeCb.val(),
			key: frm.key.val()
		});
	}
	function setValues(o){
		pp = o;
		frm.appCb.val(o.appId);
		frm.codeCb.val(o.code);
		frm.key.val(o.key);
		frm.faceCb.val(o.faceCode);
		frm.methodCb.val(o.methodCode);
	}
	function validate(p){
// 		var m = pg.seek('msg').html(''), v = App.auth.validation, x = App.validate;
// 		return x(v.func.code, p.code, m, '功能编号') &&
// 			x(v.func.name, p.name, m, '功能名称') &&
// 			x(v.func.status, p.status, m, '状态');
		return true;
	}

	pg.seek('submit').click(function(){
		var pms = getValues();
		if (!validate(pms)) {
			return;
		}
		mac.confirm('确定要生成并推送代码吗？', function(){
			$.post(App.codex.ctx + '/mac/flow/wrap/submit.do', pms, function(rs){
				var ro = mac.eval(rs);
				if (ro.success) {
					App.open({
						closeOnEsc: 1,
						ctx: 'codex',
						action: 'genPreview',
						params: ro.data
					});
				} else {
					pg.seek('msg').html(mac.msg(ro.data, ro.params));
				}
			});
		});
		return false;
	});

});
</script>
<div class="head">
	<div class="top">
		<div class="title">WrapHandler生成器</div>
		<button name="submit" class="fr">提交</button>
	</div>
	<div class="clear"></div>
	<div class="form">
		<div class="item">
			<div class="label">归属应用</div>
			<div class="combo fl" name="appCb"></div>
		</div>
		<div class="clear"></div>
		<div class="item">
			<div class="label">类名</div>
			<div class="combo fl" name="codeCb"></div>
		</div>
		<div class="clear"></div>
		<div class="item">
			<div class="label">识别码</div>
			<input type="text" name="key" value="default" disabled="true" />
		</div>
		<div class="clear"></div>
		<div class="item">
			<div class="label">目标接口</div>
			<div class="combo fl" name="faceCb"></div>
		</div>
		<div class="clear"></div>
		<div class="item">
			<div class="label">目标方法</div>
			<div class="combo fl" name="methodCb"></div>
		</div>
	</div>
	<div class="clear"></div>
</div>
<div class="body form">
	<div name="checkIn" class="checkIn">
		<fieldset>
			<legend>checkIn方法</legend>
			<div class="args" name="args"></div>
			<div class="clear"></div>
		</fieldset>
	</div>
	<div name="checkOut" class="checkOut">
		<fieldset>
			<legend>checkOut方法</legend>
			<div>
				<div class="label fl">类型</div>
				<div class="label fl" name="rtnType"></div>
				<button name="add" class="fr hidden" >添加映射</button>
			</div>
			<div class="clear"></div>
			<div class="body">
				<div class="grid" name="grid"></div>
			</div>
		</fieldset>
	</div>
</div>
<div class="arg hidden" name="arg">
	<fieldset>
		<legend>参数</legend>
		<div>
			<div class="label fl">类型</div>
			<div class="label fl" name="argType"></div>
			<button name="add" class="fr" >批量添加映射</button>
			<button name="add" class="fr" >添加单个映射</button>
		</div>
		<div class="clear"></div>
		<div class="body">
			<div class="grid" name="grid"></div>
		</div>
	</fieldset>
</div>